package com.wonders.hisdoctor.station.project.hisqueue.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wonders.his.common.exception.CustomException;
import com.wonders.his.common.utils.StringUtils;
import com.wonders.his.common.utils.validator.ValidateUtils;
import com.wonders.his.common.utils.validator.group.InsertGroup;
import com.wonders.his.framework.utils.oracle.IdUtils;
import com.wonders.his.framework.utils.oracle.OracleDateUtils;
import com.wonders.hisdoctor.station.constant.ServiceConstant;
import com.wonders.hisdoctor.station.enums.OracleSequenceEnum;
import com.wonders.hisdoctor.station.project.base.domain.InfecDrgDict;
import com.wonders.hisdoctor.station.project.base.domain.InfecInfo;
import com.wonders.hisdoctor.station.project.base.mapper.InfecDrgDictMapper;
import com.wonders.hisdoctor.station.project.base.mapper.InfecInfoMapper;
import com.wonders.hisdoctor.station.project.hisqueue.service.IContagionService;
import com.wonders.hisdoctor.station.project.hisqueue.vo.ContagionQueryVo;
import com.wonders.hisdoctor.station.project.hisqueue.vo.InfecDrgDictParentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @author zhaogongming
 * @description
 * @date 2021-06-21
 **/
@Service
public class ContagionServiceImpl implements IContagionService {

    @Autowired
    private InfecInfoMapper infecInfoMapper;

    @Autowired
    private InfecDrgDictMapper infecDrgDictMapper;

    @Autowired
    private IdUtils idUtils;

    @Autowired
    private OracleDateUtils oracleDateUtils;

    /**
     * 保存并打印
     *
     * @param entity
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public InfecInfo saveAndPrint(InfecInfo entity) {
        //【1】数据校验
        ValidateUtils.validateEntity(entity, new Class[]{InsertGroup.class});
        if (StringUtils.isEmpty(entity.getInfecA()) && StringUtils.isEmpty(entity.getInfecB()) && StringUtils.isEmpty(entity.getInfecC()) && StringUtils.isEmpty(entity.getInfecO())) {
            throw new CustomException("(甲、乙、丙、其他)类传染病必选一类！");
        }
        //【2】初次/订正
        ContagionQueryVo queryVo = new ContagionQueryVo();
        BeanUtils.copyProperties(entity, queryVo);
        InfecInfo queryResult = this.query(queryVo);
        if (null == queryResult) {
            //【3】 新增
            String infecId = idUtils.getNextVal(OracleSequenceEnum.SEQ_INFEC_INFO.getCode()).toString();
            entity.setInfecId(infecId);
            entity.setOpertorDate(oracleDateUtils.getOracleSysDate());
            this.infecInfoMapper.insert(entity);
        } else {
            if (queryResult.getInfecType().equals(entity.getInfecType())) {
                //【4】修改
                String oldInfecId = queryResult.getInfecId();
                BeanUtils.copyProperties(entity, queryResult);
                queryResult.setInfecId(oldInfecId);
                queryResult.setOpertorDate(oracleDateUtils.getOracleSysDate());
                this.infecInfoMapper.updateById(queryResult);
            } else {
                //【5】 新增
                String infecId = idUtils.getNextVal(OracleSequenceEnum.SEQ_INFEC_INFO.getCode()).toString();
                entity.setInfecId(infecId);
                entity.setOpertorDate(oracleDateUtils.getOracleSysDate());
                this.infecInfoMapper.insert(entity);
            }
        }
        //【6】查询打印信息
        return this.query(queryVo);
    }

    /**
     * 查询
     *
     * @param contagionQueryVo
     * @return
     */
    @Override
    public InfecInfo query(ContagionQueryVo contagionQueryVo) {
        QueryWrapper<InfecInfo> wrapper = new QueryWrapper<>();
        wrapper.eq(ServiceConstant.PATIENT_ID, contagionQueryVo.getPatientId());
        wrapper.eq(ServiceConstant.VISIT_DATE, contagionQueryVo.getVisitDate());
        wrapper.eq(ServiceConstant.VISIT_NO, contagionQueryVo.getVisitNo());
        wrapper.eq(ServiceConstant.OPERTOR_ID, contagionQueryVo.getOpertorId());
        wrapper.eq(ServiceConstant.HOSPITAL_CODE, contagionQueryVo.getHospitalCode());
        wrapper.orderByDesc(ServiceConstant.OPERTOR_DATE);
        if (StringUtils.isNotEmpty(contagionQueryVo.getInfecType())) {
            wrapper.eq(ServiceConstant.INFEC_TYPE, contagionQueryVo.getInfecType());
        }
        return this.infecInfoMapper.selectList(wrapper).stream().findFirst().orElse(null);
    }

    /**
     * 查询传染病
     *
     * @param drgType
     * @return
     */
    @Override
    public List<InfecDrgDictParentVo> queryInfecDrgList(String drgType) {
        QueryWrapper<InfecDrgDict> wrapper = new QueryWrapper<>();
        wrapper.eq(ServiceConstant.DRG_TYPE, drgType);
        List<InfecDrgDict> infecDrgDicts = this.infecDrgDictMapper.selectList(wrapper);
        //结果
        List<InfecDrgDictParentVo> result = new ArrayList<>();
        infecDrgDicts.stream().filter(p -> StringUtils.isEmpty(p.getParentDrgId())).forEach(t -> {
            InfecDrgDictParentVo entity = new InfecDrgDictParentVo();
            BeanUtils.copyProperties(t, entity);
            List<InfecDrgDict> collect = infecDrgDicts.stream().filter(o -> t.getDrgId().equals(StringUtils.isEmpty(o.getParentDrgId()) ? "" : o.getParentDrgId())).collect(Collectors.toList());
            entity.setChildList(collect);
            result.add(entity);
        });
        return result;
    }
}
